package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.github.talelin.latticy.mapper.*;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.pojo.*;
import io.github.talelin.latticy.service.DispatchListService;
import io.github.talelin.latticy.vo.zty.DispatchDetailVO;
import io.github.talelin.latticy.vo.zty.DispatchListVO;
import io.github.talelin.latticy.vo.zty.OutputDetailVO;
import io.github.talelin.latticy.vo.zty.OutputListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZTY
 * @since 2021-04-15
 */
@Service
public class DispatchListServiceImpl extends ServiceImpl<DispatchListMapper, DispatchList> implements DispatchListService {

    @Autowired
    DispatchListMapper dispatchListMapper;

    @Autowired
    DispatchDetailMapper dispatchDetailMapper;

    @Autowired
    OutputListMapper outputListMapper;

    @Autowired
    OutputDetailMapper outputDetailMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ReplenishListMapper replenishListMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public int createDispatchListByOutputList(int outputId, int handlerId) {
        OutputList outputList = outputListMapper.selectById(outputId);
        ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
        map.put("output_id", outputId);
        List<OutputDetail> outputDetails = outputDetailMapper.selectByMap(map);
        DispatchList dispatchList = new DispatchList();
        dispatchList.setOutputId(outputId);
//        dispatchList.setShopHandlerId(handlerId);
        dispatchList.setStatus(outputList.getStatus());
        dispatchList.setWarehouseHandlerId(handlerId);
        int insert = dispatchListMapper.insert(dispatchList);

        for (OutputDetail outputDetail: outputDetails) {
            DispatchDetail dispatchDetail = new DispatchDetail();
            dispatchDetail.setDispatchId(dispatchList.getDispatchId());
            dispatchDetail.setGoodsId(outputDetail.getGoodsId());
            dispatchDetail.setQuantity(outputDetail.getQuantity());
            dispatchDetailMapper.insert(dispatchDetail);
        }

        return insert;
    }

    @Override
    public List<DispatchListVO> getDispatchLists() {
        QueryWrapper<DispatchList> dispatchListQueryWrapper = new QueryWrapper<>();
        dispatchListQueryWrapper.orderByAsc("status").orderByDesc("create_time");
        List<DispatchList> dispatchLists = dispatchListMapper.selectList(dispatchListQueryWrapper);
        List<DispatchListVO> dispatchListVOs = new ArrayList<>();
        for (DispatchList dispatchList : dispatchLists) {
            DispatchListVO dispatchListVO = new DispatchListVO();
            OutputList outputList = outputListMapper.selectById(dispatchList.getOutputId());
            ReplenishList replenishList = replenishListMapper.selectById(outputList.getReplenishId());
            Shop shop = shopMapper.selectById(replenishList.getShopId());
            dispatchListVO.setShopName(shop.getShopName());
            UserDO warehouseHandler = userMapper.selectById(dispatchList.getWarehouseHandlerId());
            dispatchListVO.setWarehouseHandlerName(warehouseHandler.getUsername());
            if (dispatchList.getShopHandlerId() != null){
                UserDO shopHandler = userMapper.selectById(dispatchList.getShopHandlerId());
                dispatchListVO.setShopHandlerName(shopHandler.getUsername());
            }
            dispatchListVO.setCreateTime(dispatchList.getCreateTime());
            dispatchListVO.setDispatchId(dispatchList.getDispatchId());
            dispatchListVO.setOutputId(dispatchList.getOutputId());
            if (dispatchList.getStatus() == 1){
                dispatchListVO.setStatus("1");
            }else if (dispatchList.getStatus() == 2){
                dispatchListVO.setStatus("2");
            }else if (dispatchList.getStatus() == 3){
                dispatchListVO.setStatus("配送中");
            }else{
                dispatchListVO.setStatus("已送达");
            }
            dispatchListVOs.add(dispatchListVO);
        }
        return dispatchListVOs;
    }

    @Override
    public List<DispatchDetailVO> getDispatchDetailsByListId(int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("dispatch_id",id);
        List<DispatchDetail> dispatchDetails = dispatchDetailMapper.selectByMap(map);
        List<DispatchDetailVO> dispatchDetailVOs = new ArrayList<>();
        for (DispatchDetail dispatchDetail : dispatchDetails) {
            DispatchDetailVO dispatchDetailVO = new DispatchDetailVO();
            Goods goods = goodsMapper.selectById(dispatchDetail.getGoodsId());
            dispatchDetailVO.setGoodsName(goods.getGoodsName());
            dispatchDetailVO.setQuantity(dispatchDetail.getQuantity());
            dispatchDetailVO.setDispatchId(id);
            dispatchDetailVO.setDispatchDetailId(dispatchDetail.getDispatchDetailId());
            dispatchDetailVO.setInputPrice(goods.getInputPrice());
            dispatchDetailVOs.add(dispatchDetailVO);
        }
        return dispatchDetailVOs;
    }
}
